#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 1e5 + 10;

struct Seg{
    int x, y, t;
    
    bool operator < (const Seg& w) const{
        return t < w.t;
    }
}seg[N];
int n, m;
int p[N];
int cnt = 1;

int find(int x){
    if(x != p[x]){
        p[x] = find(p[x]);
    }
    return p[x];
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;
    for(int i = 1; i <= n; i ++) p[i] = i;

    for(int i = 0; i < m; i ++){
        cin >> seg[i].x >> seg[i].y >> seg[i].t;
    }

    sort(seg, seg + m);

    bool ok = false;

    if(n == 1){
        cout << 0 << '\n';
        exit(0);
    }

    for(int i = 0; i < m; i ++){
        int x = seg[i].x, y = seg[i].y, t = seg[i].t;
        int fx = find(x), fy = find(y);

        if(fx != fy){
            p[fx] = fy;
            cnt ++;
        }

        if(cnt == n){
            cout << t << '\n';
            exit(0);
        }
    }

    cout << -1 << '\n';


    return 0;
}